/*
 * Copyright 2025 The Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *@file    : rotation_local_parameterzation.h
 *@brief   : rotation_local_parameterzation
 *@authors : zhanglei
 *@version : v1.0
 *@data    : 2025/9/14
 *
 */
#ifndef ROTATION_LOCAL_PARAMETERIZATION_H_
#define ROTATION_LOCAL_PARAMETERIZATION_H_
#include <vector>
#include <memory>

#include <ceres/ceres.h>

// #define CeresLocalParameterization 1


#include "common/state.h"

namespace optimizer {

#ifndef CeresLocalParameterization

#include <ceres/manifold.h>

class RotationLocalParameterization : public ceres::Manifold  {
public:
    virtual int AmbientSize() const override { return 1; }
    virtual int TangentSize() const override { return 1; }

    virtual bool Plus(const double* x,
                     const double* delta,
                     double* x_plus_delta) const override {
        // x: 当前角度, delta: 增量, x_plus_delta: 更新后的角度
        x_plus_delta[0] = NormalizeAngle(x[0] + delta[0]);
        return true;
    }

    virtual bool PlusJacobian(const double* x, double* jacobian) const override {
        // 雅可比矩阵是 1x1 的单位矩阵
        jacobian[0] = 1.0;
        return true;
    }

    virtual bool Minus(const double* y,
                      const double* x,
                      double* y_minus_x) const override {
        // 计算两个角度之间的差异
        y_minus_x[0] = NormalizeAngle(y[0] - x[0]);
        return true;
    }

    virtual bool MinusJacobian(const double* x, double* jacobian) const override {
        // 雅可比矩阵是 1x1 的单位矩阵
        jacobian[0] = 1.0;
        return true;
    }

    static double NormalizeAngle(double angle) {
        // 高效的角度标准化
        angle = std::fmod(angle, 2.0 * M_PI);
        if (angle > M_PI) angle -= 2.0 * M_PI;
        else if (angle < -M_PI) angle += 2.0 * M_PI;
        return angle;
    }
};

#else 

#include <ceres/local_parameterization.h>

class RotationLocalParameterization : public ceres::LocalParameterization  {
public:
    virtual int GlobalSize() const { return 1; };
    virtual int LocalSize() const { return 1; };

    virtual bool Plus(const double *x, const double *delta, double *x_plus_delta) const {
        x_plus_delta[0] = NormalizeAngle(x[0] + delta[0]);  
        return true;
    }

    virtual bool ComputeJacobian(const double *x, double *jacobian) const {
        jacobian[0] = 1.0;
        return true;
    }

    static double NormalizeAngle(double angle) {
        while (angle > M_PI) angle -= 2. * M_PI;
        while (angle < -M_PI) angle += 2. * M_PI;
        return angle;
    }
};


#endif
}

#endif 
